package com.hlt.shuiliting.servlet;

import java.io.IOException;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.hlt.shuiliting.dao.CanJianQYDAO;
import com.hlt.shuiliting.dao.GongChengXMDAO;
import com.hlt.shuiliting.valuebean.CanJianQY;

public class CanJianQYServlet extends HttpServlet {

	/**
	 * Constructor of the object.
	 */
	public CanJianQYServlet() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	/**
	 * The doGet method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		response.setContentType("text/html");
		doPost(request, response);
	}

	/**
	 * The doPost method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to post.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		request.setCharacterEncoding("UTF-8");//�������Ĳ���
		response.setCharacterEncoding("UTF-8");//�������Ĳ���
		response.setContentType("text/html");
		
		String servletPath = request.getServletPath();
		if (servletPath.equalsIgnoreCase("/listCanJianQY")) {
			 listCanJianQY(request, response);
		} else if (servletPath.equalsIgnoreCase("/viewCanJianQY")) {
			viewCanJianQY(request, response);
		} else if (servletPath.equalsIgnoreCase("/insertCanJianQY")) {
			insertCanJianQY(request, response);
		} else if (servletPath.equalsIgnoreCase("/updateCanJianQY")) {
			updateCanJianQY(request, response);
		} else if (servletPath.equalsIgnoreCase("/deleteCanJianQY")) {
			deleteCanJianQY(request, response);
		}
	}
	
	public void listCanJianQY(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		CanJianQYDAO canJianQYDAO = new CanJianQYDAO();
		GongChengXMDAO gongChengXMDAO = new GongChengXMDAO();
		
		String pageSize = request.getParameter("pageSize");
		String pageNum = request.getParameter("pageNum");
		String GongChengXMID = request.getParameter("GongChengXMID");
		
		List list = canJianQYDAO.list(Integer.parseInt(GongChengXMID), (Integer.parseInt(pageSize)), (Integer.parseInt(pageNum)));
		String pageCount = Integer.toString(canJianQYDAO.getPageCount());
		Map map = (Map)gongChengXMDAO.view(Integer.parseInt(GongChengXMID));
		/**
		StringBuffer xml = new StringBuffer();
		xml.append("<table>\n" +
				"<tr>\n" +
				"<td><button onclick='insertCanJianQY()'>新增</button></td>" +
				"<td><button onclick='updateCanJianQY()'>修改</button></td>" +
				"<td><button onclick='deleteCanJianQY()'>删除</button></td>" +
				"</tr>" +
				"<tr><td>名称</td></tr>\n");
		xml.append("<tr><td>\n");
		if (list.size() != 0 && list != null) {
			for (int i = 0; i < list.size(); i++) {
				Map canJianQY = (Map) list.get(i);
				xml.append("<a href = 'viewCanJianQY?ID=" + canJianQY.get("ID") + "' onclick=>");
				xml.append(canJianQY.get("CANJIANDWMC"));
				xml.append("</a></td></tr>\n");
				xml.append("</table>\n");
			}
			
		} else {
			xml.append("<name>");
			xml.append("</name>\n");
		}

		xml.append("</canjianqy>\n");
		System.out.println(xml);
		//response.setContentType("text/xml");
		response.getWriter().write(xml.toString());
		*/
		request.setAttribute("pageCount", pageCount);
		request.setAttribute("pageNum", pageNum);
		request.setAttribute("gongChengXM", map);
		request.setAttribute("canJianQYLIST", list);
		
		request.getRequestDispatcher("/CanJianQY_List.jsp").forward(request, response);
	}
	
	public void viewCanJianQY(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		CanJianQYDAO canJianQYDAO = new CanJianQYDAO();
		String ID = request.getParameter("ID");
		String viewOrUpdate = request.getParameter("viewOrUpdate");
		Map map = canJianQYDAO.view(Integer.parseInt(ID));
		request.setAttribute("canJianQY", map);
		
		if (viewOrUpdate.equalsIgnoreCase("view")) {
			request.getRequestDispatcher("CanJianQY_View.jsp").forward(request, response);
		} else if (viewOrUpdate.equalsIgnoreCase("update")) {
			request.getRequestDispatcher("CanJianQY_Update.jsp").forward(request, response);
		}
	}
	
	public void insertCanJianQY(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		CanJianQYDAO canJianQYDAO = new CanJianQYDAO();
		CanJianQY canJianQY = new CanJianQY();
		
		String ZhuangTai = request.getParameter("ZhuangTai");
		String JiLuSJ = request.getParameter("JiLuSJ");
		String LuRuR = request.getParameter("LuRuR");
		String CanJianDWMC = request.getParameter("CanJianDWMC");
		String ZeRenLX = request.getParameter("ZeRenLX");
		String XiangMuFZR = request.getParameter("XiangMuFZR");
		String JiShuFZR = request.getParameter("JiShuFZR");
		String FuZeXMZYSX = request.getParameter("FuZeXMZYSX");
		String GongHuoZYSBMS = request.getParameter("FuZeXMZYSX");
		String JiaoHuoRQ = request.getParameter("JiaoHuoRQ");
		String GongChengXMID = request.getParameter("GongChengXMID");
		String QiYeID = request.getParameter("QiYeID");
		
		canJianQY.setZhuangTai(ZhuangTai);
		canJianQY.setJiLuSJ(JiLuSJ);
		canJianQY.setLuRuR(LuRuR);
		canJianQY.setCanJianDWMC(CanJianDWMC);
		canJianQY.setZeRenLX(ZeRenLX);
		canJianQY.setXiangMuFZR(XiangMuFZR);
		canJianQY.setJiShuFZR(JiShuFZR);
		canJianQY.setFuZeXMZYSX(FuZeXMZYSX);
		canJianQY.setGongHuoZYSBMS(GongHuoZYSBMS);
		canJianQY.setJiaoHuoRQ(JiaoHuoRQ);
		canJianQY.setGongChengXMID(Integer.parseInt(GongChengXMID));
		canJianQY.setQiYeID(Integer.parseInt(QiYeID));
		canJianQYDAO.insert(canJianQY);
		
		response.sendRedirect("listCanJianQY?GongChengXMID=" + GongChengXMID);
	}

	public void updateCanJianQY(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		CanJianQYDAO canJianQYDAO = new CanJianQYDAO();
		CanJianQY canJianQY = new CanJianQY();
		
		String ID = request.getParameter("ID");
		String ZhuangTai = request.getParameter("ZhuangTai");
		String JiLuSJ = request.getParameter("JiLuSJ");
		String LuRuR = request.getParameter("LuRuR");
		String CanJianDWMC = request.getParameter("CanJianDWMC");
		String ZeRenLX = request.getParameter("ZeRenLX");
		String XiangMuFZR = request.getParameter("XiangMuFZR");
		String JiShuFZR = request.getParameter("JiShuFZR");
		String FuZeXMZYSX = request.getParameter("FuZeXMZYSX");
		String GongHuoZYSBMS = request.getParameter("FuZeXMZYSX");
		String JiaoHuoRQ = request.getParameter("JiaoHuoRQ");
		String GongChengXMID = request.getParameter("GongChengXMID");
		String QiYeID = request.getParameter("QiYeID");
		
		canJianQY.setID(Integer.parseInt(ID));
		canJianQY.setZhuangTai(ZhuangTai);
		canJianQY.setJiLuSJ(JiLuSJ);
		canJianQY.setLuRuR(LuRuR);
		canJianQY.setCanJianDWMC(CanJianDWMC);
		canJianQY.setZeRenLX(ZeRenLX);
		canJianQY.setXiangMuFZR(XiangMuFZR);
		canJianQY.setJiShuFZR(JiShuFZR);
		canJianQY.setFuZeXMZYSX(FuZeXMZYSX);
		canJianQY.setGongHuoZYSBMS(GongHuoZYSBMS);
		canJianQY.setJiaoHuoRQ(JiaoHuoRQ);
		canJianQY.setGongChengXMID(Integer.parseInt(GongChengXMID));
		canJianQY.setQiYeID(Integer.parseInt(QiYeID));
		canJianQYDAO.update(canJianQY);
		
		response.sendRedirect("listCanJianQY?GongChengXMID=" + GongChengXMID);
	}
	
	public void deleteCanJianQY(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		CanJianQYDAO canJianQYDAO = new CanJianQYDAO();
		String ID = request.getParameter("ID");
		String GongChengXMID = request.getParameter("gongChengXMID");
		canJianQYDAO.delete(Integer.parseInt(ID));
		response.sendRedirect("listCanJianQY?GongChengXMID=" + GongChengXMID);
	}
	/**
	 * Initialization of the servlet. <br>
	 *
	 * @throws ServletException if an error occurs
	 */
	public void init() throws ServletException {
		// Put your code here
	}

}
